package com.kaoshi.myweb.snippet.mq.rabbitmq.publish;


import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
/**
 * Created by Administrator on 2017/3/4.
 * 使用主题进行消息分发
 * routing_key，支持两个特殊字符（在正则表达式里叫元字符）：
 * (星号) 代表任意 一个单词
 # (hash) 0个或者多个单词
 user.* 匹配 user.info user.error user.debug
 比dirct方式灵活
 */
public class EmitLogTopic {

	private static final String EXCHANGE_NAME = "topic_logs";

	public static void main(String[] argv)
			throws Exception {

		ConnectionFactory factory = new ConnectionFactory();
		factory.setHost("localhost");
		Connection connection = factory.newConnection();
		Channel channel = connection.createChannel();

		channel.exchangeDeclare(EXCHANGE_NAME, "topic");

		String routingKey = argv[0];
		String message = argv[1];

		channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
		System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");

		connection.close();
	}
	//...
}
